/*
 * Application.cpp
 *
 *  Created on: 2018年7月25日
 *      Author: terry
 */

#include "Application.h"
#include "TSingleton.h"


typedef comn::Singleton< Application >		ApplicationSingleton;

Application& Application::instance()
{
	return ApplicationSingleton::instance();
}


Application::Application()
{
}

Application::~Application()
{
	clear();
}


int Application::init()
{
	av::LitePlayerFactory::startup();

	return 0;
}

void Application::quit()
{
	clear();

	clearWriter();

	av::LitePlayerFactory::cleanup();
}

void Application::clear()
{
	HANDLE handle = HANDLE();
	PlayerHolderPtr player;
	while (m_playerMap.pop_front(handle, player))
	{
		player->close();
	}
}

void Application::put(HANDLE handle, PlayerHolderPtr& player)
{
	m_playerMap.put(handle, player);
}

PlayerHolderPtr Application::remove(HANDLE handle)
{
	PlayerHolderPtr player;
	m_playerMap.remove(handle, player);
	return player;
}

PlayerHolderPtr Application::get(HANDLE handle)
{
	return m_playerMap.find(handle);
}

void Application::clearWriter()
{
	HANDLE handle = HANDLE();
	MediaWriterPtr writer;
	while (m_writerMap.pop_front(handle, writer))
	{
		writer->close();
	}
}

void Application::putWriter(HANDLE handle, MediaWriterPtr& writer)
{
	m_writerMap.put(handle, writer);
}

MediaWriterPtr Application::removeWriter(HANDLE handle)
{
	MediaWriterPtr writer;
	m_writerMap.remove(handle, writer);
	return writer;
}

MediaWriterPtr Application::getWriter(HANDLE handle)
{
	return m_writerMap.find(handle);
}